package com.lg.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lg.common.pojo.ResponseResult;
import com.lg.product.entity.GroupSpec;
import com.lg.product.entity.SpecGroup;
import com.lg.product.entity.SpecParam;
import com.lg.product.service.ISpecGroupService;
import com.lg.product.service.ISpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 规格参数的分组表，每个商品分类下有多个规格参数组 前端控制器
 * </p>
 *
 * @author lzk
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/spec-group")
public class SpecGroupController {

    @Autowired
    ISpecGroupService iSpecGroupService;

    @Autowired
    ISpecParamService iSpecParamService;

    private static String flag="no";

    @RequestMapping("/glist")
    public ResponseResult glist(Long cid){
        List<SpecGroup> list = iSpecGroupService.specList(cid);
        list.forEach(group->{
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("group_id",group.getId());
            List list1 = iSpecParamService.list(wrapper);
            group.setParams(list1);
        });
        return  ResponseResult.success(list);
    }

    /*
    @RequestMapping("/edit")
    @Transactional
    public ResponseResult edit(@RequestBody List<SpecGroupVo> groupVos,Long cid){

        try {

            //规格分组添加的集合
            List<SpecGroup> insertLists=new ArrayList<>();
            //规格分组修改的集合
            List<SpecGroup> updateLists=new ArrayList<>();
            //规格参数添加的集合
            List<SpecParam> insertParams=new ArrayList<>();
            //规格参数修改的集合
            List<SpecParam> updateParams=new ArrayList<>();

            groupVos.forEach(item->{
                //分组主键如果为空则是添加
                if(item.getGid()==null){
                    flag="yes";
                    SpecGroup specGroup = new SpecGroup();
                    specGroup.setCid(cid);
                    specGroup.setName(item.getGvalue());
                    if(item.getParam().getPname().size()>0){
                        List<SpecParam> params=new ArrayList<>();
                        item.getParam().getPname().forEach(pitem->{
                            SpecParam specParam = new SpecParam();
                            specParam.setCid(cid);
                            specParam.setGroupId(item.getGid());
                            specParam.setName(pitem.getPvalue());
                            params.add(specParam);

                        });
                        specGroup.setParams(params);
                        insertLists.add(specGroup);
                    }


                }
                //如果gid不为空就是修改
                if(item.getGid()!=null){
                    SpecGroup specGroup = new SpecGroup();
                    specGroup.setId(item.getGid());
                    specGroup.setCid(cid);
                    specGroup.setName(item.getGvalue());
                    updateLists.add(specGroup);
                    item.getParam().getPname().forEach(pitem->{
                            //如果param id为空则是添加
                            if(pitem.getPid()==null){
                                SpecParam specParam = new SpecParam();
                                specParam.setCid(cid);
                                specParam.setGroupId(item.getGid());
                                specParam.setName(pitem.getPvalue());
                                insertParams.add(specParam);
                            }
                            //如果param id不为空则是修改
                            if(pitem.getPid()!=null){
                                SpecParam specParam = new SpecParam();
                                specParam.setId(pitem.getPid());
                                specParam.setCid(pitem.getCid());
                                specParam.setGroupId(item.getGid());
                                specParam.setName(pitem.getPvalue());
                                updateParams.add(specParam);
                            }
                    });
                }

            });

            //添加规格分组
            boolean gb1 = iSpecGroupService.saveBatch(insertLists);

            System.out.println(insertLists);


            //讲已经添加的规格分组的主键id赋值给规格参数的group_id
            if(flag.equals("yes")){
                insertLists.forEach(item->{
                    item.getParams().forEach(pitem->{
                        pitem.setGroupId(item.getId());
                        insertParams.add(pitem);
                    });
                });
            }

            System.out.println(insertParams);

            //修改规格分组
            boolean gb2 = iSpecGroupService.updateBatchById(updateLists);
            //添加规格参数
            boolean pb1 = iSpecParamService.saveBatch(insertParams);
            //修改规格参数
            boolean bp2 = iSpecParamService.updateBatchById(updateParams);
            //修改规格分组  添加规格参数

            return ResponseResult.success(true);

        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseResult.error("编辑规格错误");
    }
    */

    @RequestMapping("/deleteGroup")
    @Transactional
    public ResponseResult deleteGroup(Long gid){

        try{
            //删除分组
            boolean b = iSpecGroupService.removeById(gid);
            //删除分组下的参数
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("group_id",gid);

            boolean b2 = iSpecParamService.remove(wrapper);
            if(b){
                return ResponseResult.success(true);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseResult.error("删除失败");
    }


    @RequestMapping("/deleteParam")
    public ResponseResult deleteParam(Long pid){

        try{
            //删除规格参数
            boolean b = iSpecParamService.removeById(pid);

            if(b){
                return ResponseResult.success(true);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return ResponseResult.error("删除失败");
    }

    @RequestMapping("/save")
    @Transactional
    public ResponseResult save(@RequestBody GroupSpec groupSpec){


        ArrayList<SpecParam> addSpecParams = new ArrayList<>();

        ArrayList<SpecParam> updateSpecParams = new ArrayList<>();

        groupSpec.getGroups().forEach(group->{
            //如果规格分组id为null为添加
            if(group.getId()==null){
                group.setCid(groupSpec.getCid());
                boolean save = iSpecGroupService.save(group);
                group.getParams().forEach(param->{
                        param.setGroupId(group.getId());
                        param.setCid(groupSpec.getCid());
                    addSpecParams.add(param);
                });
            }else{
                boolean update = iSpecGroupService.updateById(group);
                group.getParams().forEach(param->{
                    //添加规格参数
                    if(param.getId()==null){
                        param.setGroupId(group.getId());
                        param.setCid(groupSpec.getCid());
                        addSpecParams.add(param);
                    }
                    //修改规格参数
                    else{
                        param.setGroupId(group.getId());
                        param.setCid(groupSpec.getCid());
                        updateSpecParams.add(param);
                    }
                });
            }
        });

        System.out.println(addSpecParams);

        if(addSpecParams.size()>0){
            boolean b = iSpecParamService.saveBatch(addSpecParams);
        }
        if(updateSpecParams.size()>0){
            boolean b = iSpecParamService.updateBatchById(updateSpecParams);
        }

        return ResponseResult.success(true);
    }
}
